package com.sugar.subscribe.course.module.business.dal.mysql.coach;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.sugar.subscribe.course.framework.mybatis.core.mapper.BaseMapperX;
import com.sugar.subscribe.course.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.sugar.subscribe.course.module.business.controller.app.coach.vo.AppBizCoachDetailRespVO;
import com.sugar.subscribe.course.module.business.controller.app.coach.vo.AppBizCoachHomePageReqVO;
import com.sugar.subscribe.course.module.business.controller.app.coach.vo.AppBizCoachHomePageRespVO;
import com.sugar.subscribe.course.module.business.dal.dataobject.coach.CoachDO;
import com.sugar.subscribe.course.module.business.dal.dataobject.coachservice.CoachServiceDO;
import com.sugar.subscribe.course.module.business.dal.dataobject.coachvenue.CoachVenueDO;
import org.apache.ibatis.annotations.Mapper;
import com.sugar.subscribe.course.module.business.controller.admin.coach.vo.*;
import org.apache.ibatis.annotations.Update;

/**
 * 教练信息 Mapper
 *
 * @author Ysugar
 */
@Mapper
public interface CoachMapper extends BaseMapperX<CoachDO> {

    default PageResult<CoachDO> selectPage(CoachPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CoachDO>()
                .eqIfPresent(CoachDO::getUserId, reqVO.getUserId())
                .likeIfPresent(CoachDO::getNickname, reqVO.getNickname())
                .likeIfPresent(CoachDO::getName, reqVO.getName())
                .eqIfPresent(CoachDO::getMobile, reqVO.getMobile())
                .eqIfPresent(CoachDO::getGender, reqVO.getGender())
                .eqIfPresent(CoachDO::getAuditStatus, reqVO.getAuditStatus())
                .eqIfPresent(CoachDO::getIsWork, reqVO.getIsWork())
                .betweenIfPresent(CoachDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CoachDO::getId));
    }

    default PageResult<AppBizCoachHomePageRespVO> appSelectCoachHomePage(AppBizCoachHomePageReqVO reqVO){
        MPJLambdaWrapperX<CoachDO> wrapper = new MPJLambdaWrapperX<>();
        wrapper.selectAs(CoachDO::getId, AppBizCoachHomePageRespVO::getId)
                .selectAs(CoachDO::getNickname, AppBizCoachHomePageRespVO::getNickname)
                .selectAs(CoachDO::getAvatar, AppBizCoachHomePageRespVO::getAvatar)
                .selectAs(CoachDO::getGender, AppBizCoachHomePageRespVO::getGender)
                .selectAs(CoachDO::getTotalScore, AppBizCoachHomePageRespVO::getTotalScore)
                .selectAs(CoachDO::getPerCapitaPrice, AppBizCoachHomePageRespVO::getPerCapitaPrice)
                .selectAs(CoachDO::getAvgAcceptTime, AppBizCoachHomePageRespVO::getAvgAcceptTime)
                .selectAs(CoachDO::getTags, AppBizCoachHomePageRespVO::getTags)
                // 获取评论数
                .selectAs("(select count(bcc.id) from business_coach_comment bcc where bcc.deleted = 0 and bcc.audit_status = 10 and bcc.coach_id = t.id)",
                        AppBizCoachHomePageRespVO::getCommentCount)
                // 获取课程最低价格
                .selectAs("(select min(bcp.price) / 100 from business_coach_plan bcp where bcp.deleted = 0 and bcp.audit_status = 10 and bcp.coach_id = t.id)",
                        AppBizCoachHomePageRespVO::getMinPrice)
                // 获取最近一条评论内容
                .selectAs("(select bcc2.content from business_coach_comment bcc2 where bcc2.deleted = 0 and bcc2.audit_status = 10 and bcc2.coach_id = t.id order by bcc2.id desc limit 1)",
                        AppBizCoachHomePageRespVO::getCommentContent)
                .eqIfPresent(CoachDO::getHomeRecommend, reqVO.getHomeRecommend())
                .eqIfPresent(CoachDO::getGender, reqVO.getGender())
                .eqIfPresent(CoachDO::getAuditStatus, 10)
                .eqIfPresent(CoachDO::getIsWork, 1);

        Long servicePId = reqVO.getServicePId();
        Long serviceId = reqVO.getServiceId();
        if (servicePId != null || serviceId != null) {
            wrapper.distinct()
                    .leftJoin(CoachServiceDO.class, CoachServiceDO::getCoachId, CoachDO::getId)
                    .eqIfPresent(CoachServiceDO::getServicePId, reqVO.getServicePId())
                    .eqIfPresent(CoachServiceDO::getServiceId, reqVO.getServiceId());
        }

        Long venueId = reqVO.getVenueId();
        if (null != venueId){
            wrapper.leftJoin(CoachVenueDO.class, CoachVenueDO::getCoachId, CoachDO::getId)
                    .eqIfPresent(CoachVenueDO::getVenueId, venueId);
        }

        Boolean scoreSort = reqVO.getScoreSort();
        if (null != scoreSort){
            wrapper.orderBy(true, reqVO.getScoreSort(), CoachDO::getTotalScore);
        }

        Boolean avgPriceSort = reqVO.getAvgPriceSort();
        if (null != avgPriceSort){
            wrapper.orderBy(true, avgPriceSort, CoachDO::getPerCapitaPrice);
        }

        return selectJoinPage(reqVO, AppBizCoachHomePageRespVO.class, wrapper);
    }

    default AppBizCoachDetailRespVO appGetCoachDetail(Long coachId){
        return selectJoinOne(AppBizCoachDetailRespVO.class, new MPJLambdaWrapperX<CoachDO>()
                .selectAll(CoachDO.class)
                // 获取评论数
                .selectAs("(select count(bcc.id) from business_coach_comment bcc where bcc.deleted = 0 and bcc.audit_status = 10 and bcc.coach_id = t.id)",
                        AppBizCoachDetailRespVO::getCommentCount)
                // 获取课程最低价格
                .selectAs("(select min(bcp.price) / 100 from business_coach_plan bcp where bcp.deleted = 0 and bcp.audit_status = 10 and bcp.coach_id = t.id)",
                        AppBizCoachDetailRespVO::getMinPrice)
                // 获取最近一条评论内容
                .selectAs("(select bcc2.content from business_coach_comment bcc2 where bcc2.deleted = 0 and bcc2.audit_status = 10 and bcc2.coach_id = t.id order by bcc2.id desc limit 1)",
                        AppBizCoachDetailRespVO::getCommentContent)
                // 获取最近最新展示视频
                .selectAs("(select bcv.video_url from business_coach_video bcv where bcv.deleted = 0 and bcv.audit_status = 10 and bcv.coach_id = t.id order by bcv.id desc limit 1)",
                        AppBizCoachDetailRespVO::getLastVideo)
                .eq(CoachDO::getId, coachId));
    }

    @Update("select sum(received_minutes) from business_order bo where bo.deleted = 0 and bo.coach_id = #{param1}")
    Long getCoachAvgReceivedOrderDuration(Long coachId);
}